﻿var maphieuxuatbanle = "";
$(function () {
    $("#ddlKho_Khoa").parent().find(".dropdown-toggle").focus();
    $("#txtTuNgay").data("DateTimePicker").setMaxDate($("#txtDenNgay").val());
    $("#txtDenNgay").data("DateTimePicker").setMinDate($("#txtTuNgay").val());
    $("#txtTuNgay").on("dp.change", function (e) {
        $("#txtDenNgay").data("DateTimePicker").setMinDate(e.date);
    });
    $("#txtDenNgay").on("dp.change", function (e) {
        $("#txtTuNgay").data("DateTimePicker").setMaxDate(e.date);
    });
    $("#txtDenNgay").bind("keydown", function (e) {
        if (e.which === 13) {
            e.preventDefault();
            $("#btnTimKiem").focus();
        }
    });
    $("#div_phieuxuatbanle").on("hide", function () {
        window.__doPostBack("btnTimKiem_Click", "");
    });
    $("#div_phieuxuatbanle_view").on("hide", function () {
        maphieuxuatbanle = "";
    });
    $("#txtDienGiai").bind("keydown", function (e) {
        if (e.which === 13) {
            e.preventDefault();
            $("#btnLuuPhieu").focus();
        }
    });
    $("#txtSoLuong").bind("keydown", function (e) {
        if (e.which === 13) {
            e.preventDefault();
            $("#btnLuuPhieu_ChiTiet").focus();
        }
    });
    $("#ddlLoaiKhachHang").on("change", function () {
        filldropdown("#ddlKhachHang", SelectKhachHangByMaLoaiKhachHang($("#ddlLoaiKhachHang option:selected").val()));
        $("#ddlKhachHang").parent().find(".dropdown-toggle").focus();
    });
    $("#ddlVatTu").on("change", function () {
        var data = SelectVatTuById($(this).val());
        if (data.length <= 0) return;
        $("#ddDonVi").selectpicker("val", data[0].MaDonVi);
        $("#ddDonVi").selectpicker("refresh");
        $("#txtSoLuongTon").val(CheckTonThucTe_MaKho_MaVatTu($("#ddlKho option:selected").val(), $("#ddlVatTu option:selected").val()));
        $("#txtSoLuong").focus();
    });
    $("#ul_tab_phieuxuatbanle a[href=\"#tab_phieuxuatbanle\"]").on("shown.bs.tab", function (e) {
        $("#txtNguoiGiao").focus();
    });
    $("#ul_tab_phieuxuatbanle a[href=\"#tab_phieuxuatbanle_chitiet\"]").on("shown.bs.tab", function (e) {
        $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
    });
});
function loadcombobox() {
    filldropdown("#ddlKho", superadmin === true ? SelectAllKho() : SelectKhoByKhoa(makhoa));
    filldropdown("#ddlLoaiKhachHang", SelectAllLoaiKhachHang());
    filldropdown("#ddDonVi", SelectAllDonVi());
    $("#ddDonVi").find("[value=\"\"]").text("");
    $("#ddDonVi").selectpicker("refresh");
}
function validationform_Timkiem() {
    $("#txtTuNgay").rules("add", {
        required: true,
        messages: {
            required: MsErrorEmpty
        }
    });
    $("#txtDenNgay").rules("add", {
        required: true,
        messages: {
            required: MsErrorEmpty
        }
    });
}
function themphieuxuatbanle() {
    if (CheckSession() === "") { window.location = "/"; return; }
    loadcombobox();
    $("#ul_tab_phieuxuatbanle a[href=\"#tab_phieuxuatbanle\"]").tab("show");
    loaddanhsachvattu_phieuxuatbanle(maphieuxuatbanle);
    $("#div_phieuxuatbanle").modal("show");
}
function suaphieuxuatbanle(tagButton) {
    if (CheckSession() === "") { window.location = "/"; return; }
    $("#btnNhapLaiPhieu").hide();
    loadcombobox();
    maphieuxuatbanle = $(tagButton).attr("maphieu");
    if (maphieuxuatbanle === "") return;
    var dataPhieuXuatBanLe = PhieuXuatBanLe_SelectById(maphieuxuatbanle);
    (dataPhieuXuatBanLe[0].NgayGiao == null || dataPhieuXuatBanLe[0].NgayGiao === "")
        ? $("#txtNgayGiao").val("")
        : $("#txtNgayGiao").data("DateTimePicker").setDate(moment(dataPhieuXuatBanLe[0].NgayGiao).format("DD-MM-YYYY"));
    $("#txtNguoiGiao").val(dataPhieuXuatBanLe[0].NguoiGiao);
    $("#ddlKho").selectpicker("val", dataPhieuXuatBanLe[0].MaKho);
    $("#ddlLoaiKhachHang").selectpicker("val", dataPhieuXuatBanLe[0].MaLoaiKhachHang).change();
    $("#ddlKhachHang").selectpicker("val", dataPhieuXuatBanLe[0].MaKhachHang);
    $("#txtDienGiai").val(dataPhieuXuatBanLe[0].DienGiai);
    $("#ddlKho").prop("disabled", true);
    $("#ddlKho").selectpicker("refresh");
    filldropdown("#ddlVatTu", SelectVatTuByMaKho($("#ddlKho option:selected").val()));
    loaddanhsachvattu_phieuxuatbanle(maphieuxuatbanle);
    $("#ul_tab_phieuxuatbanle a[href=\"#tab_phieuxuatbanle\"]").tab("show");
    $("#div_phieuxuatbanle").modal("show");
}
function luuphieuxuatbanle() {
    if (CheckSession() === "") { window.location = "/"; return; }
    $("#txtNgayGiao, #txtNguoiGiao, #ddKho, #ddlKhachHang").each(function () {
        $(this).rules("add", {
            required: true,
            messages: {
                required: MsErrorEmpty
            }
        });
    });
    if (!$("#form_phieuxuatbanle_thongtinphieu").valid()) return;
    if (maphieuxuatbanle === "") {
        maphieuxuatbanle = PhieuXuatBanLe_Insert(
            $("#ddlKho option:selected").val(), $("#txtNgayGiao").val(), $("#txtNguoiGiao").val(),
            $("#ddlKhachHang option:selected").val(), $("#txtDienGiai").val(), username);
        if (maphieuxuatbanle === "") {
            $("#txtNguoiGiao").focus();
            return;
        }
    } else {
        PhieuXuatBanLe_Update(
            $("#ddlKho option:selected").val(), maphieuxuatbanle, $("#txtNgayGiao").val(),
            $("#txtNguoiGiao").val(), $("#ddlKhachHang option:selected").val(), $("#txtDienGiai").val(), username);
    }
    $("#ddlKho").prop("disabled", true);
    $("#ddlKho").selectpicker("refresh");
    $("#btnNhapLaiPhieu").hide();
    filldropdown("#ddlVatTu", SelectVatTuByMaKho($("#ddlKho option:selected").val()));
    $("#ul_tab_phieuxuatbanle a[href=\"#tab_phieuxuatbanle_chitiet\"]").tab("show");
    $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
}
function resetphieuxuatbanle() {
    ResetControlForm("#form_phieuxuatbanle_thongtinphieu");
    $("#txtNguoiGiao").focus();
}
function suaphieuxuatbanle_chitiet(tagInput) {
    if (CheckSession() === "") { window.location = "/"; return; }
    var mavattu = $(tagInput).attr("maphieuxuatbanle_chitiet_mavattu");
    if (mavattu === "") return;
    var data = PhieuXuatBanLe_ChiTiet_SelectByMaPhieuMaVatTu(maphieuxuatbanle, mavattu);
    if (data.length <= 0) {
        bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        return;
    }
    $("#ul_tab_phieuxuatbanle a[href=\"#tab_phieuxuatbanle_chitiet\"]").tab("show");
    $("#ddlVatTu").selectpicker("val", mavattu).change();
    $("#txtSoLuong").val(data[0].SoLuong);
}
function luuphieuxuatbanle_chitiet() {
    if (CheckSession() === "") { window.location = "/"; return; }
    $("#ddlVatTu, #txtSoLuong").each(function () {
        $(this).rules("add", {
            required: true,
            messages: {
                required: MsErrorEmpty
            }
        });
    });
    $("#txtSoLuong").rules("add", {
        larger_ton: "#txtSoLuongTon"
    });
    if (!$("#form_phieuxuatbanle_chitietphieu").valid()) return;
    if (maphieuxuatbanle !== "") {
        PhieuXuatBanLe_ChiTiet_Insert(maphieuxuatbanle, $("#ddlVatTu option:selected").val(), $("#txtSoLuong").val(), username);
        loaddanhsachvattu_phieuxuatbanle(maphieuxuatbanle);
        resetphieuxuatbanlechitiet();
    } else {
        bootbox.alert("Bạn chưa tạo phiếu hủy, không thể thêm vật tư mới!");
        $("#txtNguoiGiao").focus();
    }
}
function xoaphieuxuatbanle_chitiet(tagInput, multiDelete) {
    if (CheckSession() === "") { window.location = "/"; return; }
    if (multiDelete) {
        var arrId = $("#tb_phieuxuatbanle").bootstrapTable("getSelections");
        if (arrId.length > 0) {
            bootbox.confirm("Bạn có chắc chắn xóa?", function (result) {
                if (result) {
                    $.each(arrId, function (key, value) {
                        PhieuXuatBanLe_ChiTiet_Delete(maphieuxuatbanle, value.MaVatTu);
                    });
                    loaddanhsachvattu_phieuxuatbanle(maphieuxuatbanle);
                }
            });
        } else {
            bootbox.alert("Bạn cần chọn bản ghi trước khi xóa!");
        }
    } else {
        bootbox.confirm("Bạn có chắc chắn xóa?", function (result) {
            if (result) {
                PhieuXuatBanLe_ChiTiet_Delete(maphieuxuatbanle, $(tagInput).attr("maphieuxuatbanle_chitiet_mavattu"));
                loaddanhsachvattu_phieuxuatbanle(maphieuxuatbanle);
            }
        });
    }
    $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
}
function loaddanhsachvattu_phieuxuatbanle(maphieuxuatbanle) {
    var data = PhieuXuatBanLe_ChiTiet_SelectByMaPhieu(maphieuxuatbanle);
    $("#tb_phieuxuatbanle").bootstrapTable("destroy");
    $("#tb_phieuxuatbanle").bootstrapTable({
        classes: "table table-condensed",
        data: data,
        cache: false,
        pagination: true,
        search: true,
        searchAlign: "right",
        formatShowingRows: function (pageFrom, pageTo, totalRows) {
            return "";
        },
        columns: [{
            checkbox: true
        }, {
            field: "TenVatTu",
            title: "Tên vật tư",
            align: "left",
            valign: "middle"
        }, {
            field: "TenDonVi",
            title: "Đơn vị",
            align: "center",
            valign: "middle"
        }, {
            field: "SoLuong",
            title: "Số lượng",
            align: "right",
            valign: "middle",
            formatter: function (value) {
                return $.number(value, 0, ".", ",");
            }
        }, {
            field: "MaVatTu",
            title: "",
            align: "center",
            valign: "middle",
            formatter: function (value) {
                return "<button type=\"button\" id=\"btnSuaPhieuXuatBanLe_ChiTiet\" title=\"Sửa\" maphieuxuatbanle_chitiet_mavattu=\"" + value + "\" onclick=\"suaphieuxuatbanle_chitiet(this)\" class=\"btn btn-info\"><span class=\"glyphicon glyphicon-edit\"></span></button>" +
                    "&nbsp;" +
                    "<button type=\"button\" id=\"btnXoaPhieuXuatBanLe_ChiTiet\" title=\"Xóa\" maphieuxuatbanle_chitiet_mavattu=\"" + value + "\" onclick=\"xoaphieuxuatbanle_chitiet(this, false)\" class=\"btn btn-danger\"><span class=\"glyphicon glyphicon-trash\"></span></button>";
            }
        }]
    });
    var dataChitiet = PhieuXuatBanLe_ChiTiet_SelectByMaPhieu_ChiTiet(maphieuxuatbanle);
    $("#tb_phieuxuatbanle_chitiet").bootstrapTable("destroy");
    $("#tb_phieuxuatbanle_chitiet").bootstrapTable({
        classes: "table table-condensed",
        data: dataChitiet,
        cache: false,
        pagination: true,
        search: true,
        searchAlign: "right",
        formatShowingRows: function (pageFrom, pageTo, totalRows) {
            return "";
        },
        columns: [{
            field: "TenVatTu",
            title: "Tên vật tư",
            align: "left",
            valign: "middle"
        }, {
            field: "TenDonVi",
            title: "Đơn vị",
            align: "center",
            valign: "middle"
        }, {
            field: "SoLuong",
            title: "Số lượng",
            align: "right",
            valign: "middle",
            formatter: function (value) {
                return $.number(value, 0, ".", ",");
            }
        }, {
            field: "DonGia",
            title: "Đơn giá",
            align: "right",
            valign: "middle",
            formatter: function (value) {
                return $.number(value, 0, ".", ",");
            }
        }, {
            field: "LoSanXuat",
            title: "Lô sản xuất",
            align: "center",
            valign: "middle"
        }, {
            field: "HanSuDung",
            title: "Hạn sử dụng",
            align: "center",
            valign: "middle",
            formatter: function (value) {
                return moment(value).format("DD-MM-YYYY");
            }
        }, {
            field: "MaPhieuNhap",
            title: "Mã phiếu nhập",
            align: "center",
            valign: "middle"
        }]
    });
}
function resetphieuxuatbanlechitiet() {
    ResetControlForm("#form_phieuxuatbanle_chitietphieu");
    $("#ddlVatTu").parent().find(".dropdown-toggle").focus();
    filldropdown("#ddlLoSanXuat", []);
    filldropdown("#ddlPhieuNhap", []);
}
function viewphieuxuatbanle(tagButton) {
    maphieuxuatbanle = $(tagButton).attr("maphieu");
    if (maphieuxuatbanle === "") return;
    var dataPhieuXuatBanLe = PhieuXuatBanLe_SelectById(maphieuxuatbanle);
    (dataPhieuXuatBanLe[0].NgayGiao == null || dataPhieuXuatBanLe[0].NgayGiao === "")
        ? $("#txtNgayGiao_ViewPhieuXuatBanLe").val("")
        : $("#txtNgayGiao_ViewPhieuXuatBanLe").val(moment(dataPhieuXuatBanLe[0].NgayGiao).format("DD-MM-YYYY"));
    $("#txtNguoiGiao_ViewPhieuXuatBanLe").val(dataPhieuXuatBanLe[0].NguoiGiao);
    $("#txtLoaiKhachHang_ViewPhieuXuatBanLe").val(dataPhieuXuatBanLe[0].TenLoaiKhachHang);
    $("#txtKhachHang_ViewPhieuXuatBanLe").val(dataPhieuXuatBanLe[0].TenKhachHang);
    $("#txtKho_ViewPhieuXuatBanLe").val(dataPhieuXuatBanLe[0].TenKho);
    $("#txtDienGiai_ViewPhieuXuatBanLe").val(dataPhieuXuatBanLe[0].DienGiai);
    // Thông tin thuốc-vật tư
    var data = PhieuXuatBanLe_ChiTiet_SelectByMaPhieu(maphieuxuatbanle);
    $("#tb_phieuxuatbanle_view").bootstrapTable("destroy");
    $("#tb_phieuxuatbanle_view").bootstrapTable({
        classes: "table table-condensed",
        data: data,
        cache: false,
        pagination: true,
        search: true,
        searchAlign: "right",
        formatShowingRows: function (pageFrom, pageTo, totalRows) {
            return "";
        },
        columns: [{
            field: "TenVatTu",
            title: "Tên vật tư",
            align: "left",
            valign: "middle"
        }, {
            field: "TenDonVi",
            title: "Đơn vị",
            align: "center",
            valign: "middle"
        }, {
            field: "SoLuong",
            title: "Số lượng",
            align: "right",
            valign: "middle",
            formatter: function (value) {
                return $.number(value, 0, ".", ",");
            }
        }]
    });
    var dataChitiet = PhieuXuatBanLe_ChiTiet_SelectByMaPhieu_ChiTiet(maphieuxuatbanle);
    $("#tb_phieuxuatbanle_view_chitiet").bootstrapTable("destroy");
    $("#tb_phieuxuatbanle_view_chitiet").bootstrapTable({
        classes: "table table-condensed",
        data: dataChitiet,
        cache: false,
        pagination: true,
        search: true,
        searchAlign: "right",
        formatShowingRows: function (pageFrom, pageTo, totalRows) {
            return "";
        },
        columns: [{
            field: "TenVatTu",
            title: "Tên vật tư",
            align: "left",
            valign: "middle"
        }, {
            field: "TenDonVi",
            title: "Đơn vị",
            align: "center",
            valign: "middle"
        }, {
            field: "SoLuong",
            title: "Số lượng",
            align: "right",
            valign: "middle",
            formatter: function (value) {
                return $.number(value, 0, ".", ",");
            }
        }, {
            field: "DonGia",
            title: "Đơn giá",
            align: "right",
            valign: "middle",
            formatter: function (value) {
                return $.number(value, 0, ".", ",");
            }
        }, {
            field: "LoSanXuat",
            title: "Lô sản xuất",
            align: "center",
            valign: "middle"
        }, {
            field: "HanSuDung",
            title: "Hạn sử dụng",
            align: "center",
            valign: "middle",
            formatter: function (value) {
                return moment(value).format("DD-MM-YYYY");
            }
        }, {
            field: "MaPhieuNhap",
            title: "Mã phiếu nhập",
            align: "center",
            valign: "middle"
        }]
    });
    $("#div_phieuxuatbanle_view").modal("show");
}
function PhieuXuatBanLe_Insert(makho, ngaygiao, nguoigiao, makhachhang, diengiai, nguoitao) {
    var maphieu = "";
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuXuatBanLe.asmx/PhieuXuatBanLe_Insert",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'makho': '" + makho + "', 'ngaygiao': '" + ngaygiao + "'" +
            ", 'nguoigiao': '" + nguoigiao + "', 'makhachhang': '" + makhachhang + "'" +
            ", 'diengiai': '" + diengiai + "', 'nguoitao': '" + nguoitao + "'}",
        async: false,
        success: function (n) {
            maphieu = n.d;
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return maphieu;
}
function PhieuXuatBanLe_Update(makho, maphieu, ngaygiao, nguoigiao, makhachhang, diengiai, nguoitao) {
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuXuatBanLe.asmx/PhieuXuatBanLe_Update",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'makho': '" + makho + "', 'maphieu': '" + maphieu + "', 'ngaygiao': '" + ngaygiao + "'" +
            ", 'nguoigiao': '" + nguoigiao + "' , 'makhachhang': '" + makhachhang + "'" +
            ", 'diengiai': '" + diengiai + "', 'nguoitao': '" + nguoitao + "'}",
        async: false,
        success: function () {
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
}
function PhieuXuatBanLe_SelectById(maphieu) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuXuatBanLe.asmx/PhieuXuatBanLe_SelectById",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuXuatBanLe_ChiTiet_Insert(maphieu, mavattu, soluong, nguoitao) {
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuXuatBanLe.asmx/PhieuXuatBanLe_ChiTiet_Insert",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "', 'mavattu': '" + mavattu + "'" +
            ", 'soluong': " + parseInt(soluong) + ", 'nguoitao': '" + nguoitao + "'}",
        async: false,
        success: function () {
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
}
function PhieuXuatBanLe_ChiTiet_SelectByMaPhieu(maphieu) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuXuatBanLe.asmx/PhieuXuatBanLe_ChiTiet_SelectByMaPhieu",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuXuatBanLe_ChiTiet_SelectByMaPhieu_ChiTiet(maphieu) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuXuatBanLe.asmx/PhieuXuatBanLe_ChiTiet_SelectByMaPhieu_ChiTiet",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuXuatBanLe_ChiTiet_SelectByMaPhieuMaVatTu(maphieu, mavattu) {
    var arr = [];
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuXuatBanLe.asmx/PhieuXuatBanLe_ChiTiet_SelectByMaPhieuMaVatTu",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "', 'mavattu': '" + mavattu + "'}",
        async: false,
        success: function (n) {
            if (n.d != null || n.d != undefined) { arr = $.parseJSON(n.d); }
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
    return arr;
}
function PhieuXuatBanLe_ChiTiet_Delete(maphieu, mavattu) {
    $.ajax({
        type: "POST",
        url: "/Webservice/QuanLyDuoc/HeThongNhapXuat/wsPhieuXuatBanLe.asmx/PhieuXuatBanLe_ChiTiet_Delete",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: "{'maphieu': '" + maphieu + "', 'mavattu': '" + mavattu + "'}",
        async: false,
        success: function () {
        },
        error: function () {
            bootbox.alert("Có lỗi xảy ra, vui lòng thử lại");
        }
    });
}